package com.example.arithmeticleetcode.leetcode.y2023;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.http.RequestEntity;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author ZHEN.FANG@opple.com
 * @create 2023-01-15 16:52
 * @Description
 **/
public class SendMsg {

    public static void main(String[] args) {
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(1);
        threadPool.scheduleWithFixedDelay(() -> {
            try {
                DeviceMsgResp msgResp = getHttp();
                if (msgResp != null) {
                    if (Double.parseDouble(msgResp.getResult().replace("%", "")) <= 30.0) {
                        postHttp("监测项目当前设备在线率为:" + msgResp.getResult());
                    }
                }
            } catch (Exception e) {
                System.out.println("执行整个链路异常" + e.getMessage());
            }
        }, 1, 1 * 60 * 60, TimeUnit.SECONDS);


    }

    public static void postHttp(String content) {
        CloseableHttpClient httpClient = null;
        try {
            // 1.请求URL
            String postUrl = "http://10.10.90.243:18822/baseservice/Common/SendSms";
            // 2.请求参数
            Map<String, Object> map = new HashMap<>();
            map.put("phoneNos", List.of("13721027893"));
            map.put("content", content);
            String json = JSON.toJSONString(map);
            // 3.创建连接与设置连接参数
            httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(postUrl);
            StringEntity entity = new StringEntity(json, Charset.forName("UTF-8"));
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            // 4.发起请求与接收返回值
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() != 200) {
                System.out.println("发送短信异常,发送数据:" + json);
                return;
            }
            HttpEntity res = response.getEntity();
            String resultData = EntityUtils.toString(res);
            System.out.println("从服务端返回结果: " + resultData);
        } catch (Exception e) {
            System.out.println("发送短信异常,原因:" + e.getMessage());
            return;
        } finally {
            // 5.关闭连接
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (Exception e) {
                    System.out.println("发送短信异常关闭连接,原因:" + e.getMessage());
                    return;
                }
            }
        }
    }

    public static DeviceMsgResp getHttp() {
        CloseableHttpClient httpClient = null;
        try {
            // 1.请求URL与组装请求参数
            String getUrl = "https://iotservices.opple.com/api/Common/GetOnlineRate";
            // 2.创建连接与设置连接参数
            httpClient = HttpClientBuilder.create().build();
            HttpGet httpGet = new HttpGet(getUrl);
            // 3.发起请求与接收返回值
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() != 200) {
                System.out.println("调用查询设备接口异常,返回结果:" + JSON.toJSONString(response));
                postHttp("调用查询设备接口异常,返回结果:" + JSON.toJSONString(response));
                return null;
            }
            HttpEntity res = response.getEntity();
            String resultData = EntityUtils.toString(res);
            System.out.println("从服务端返回结果: " + resultData);
            DeviceMsgResp deviceMsgResp = JSON.parseObject(resultData, DeviceMsgResp.class);
            return deviceMsgResp;
        } catch (Exception e) {
            System.out.println("调用查询设备接口异常,原因:" + e.getMessage());
            postHttp("调用查询设备接口异常,原因:" + e.getMessage());
            return null;
        } finally {
            // 4.关闭连接
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (Exception e) {
                    System.out.println("调用查询设备关闭连接异常,原因:" + e.getMessage());
                    postHttp("调用查询设备关闭连接异常,原因:" + e.getMessage());
                }
            }
        }
    }
}

class DeviceMsgResp {
    private String Result;

    private Integer StatusCode;

    private String Message;

    public String getResult() {
        return Result;
    }

    public void setResult(String result) {
        Result = result;
    }

    public Integer getStatusCode() {
        return StatusCode;
    }

    public void setStatusCode(Integer statusCode) {
        StatusCode = statusCode;
    }

    public String getMessage() {
        return Message;
    }

    public void setMessage(String message) {
        Message = message;
    }
}
